# -*- utf-8 -*-
from apps.data_visual.models import FieldNameAlias
from apps.data_visual.serializers import FieldNameAliasSerializer
from apps.data_visual.sqla_models import get_fields


def field_alias_list(table_name):
    field_names = get_fields(table_name).keys()
    field_name_alias = FieldNameAlias.objects.filter(table_name=table_name, is_delete=False).order_by(
        'create_time')
    serializer = FieldNameAliasSerializer(field_name_alias, many=True)
    ret = [{'column': i, 'alias': i} for i in field_names]
    iter_data = serializer.data
    for item in iter_data:
        for index, value in enumerate(ret):
            if item['field_name'] == value['column']:
                ret[index]['alias'] = item['alias']
    return ret
